// order-detail
import { PACKAGE_ACTIVITY_PAGE } from '../../../constants/route';
import { handleBrandOrderStatus } from '../../biztools/order';
import { showLoading, hideLoading, tipError } from '../../../common/env';
import { DELIVERY_TYPE_ENUM, ORDER_TYPE } from '../../../constants/order';
import { getBrandOrderDetail, deliverBrandOrder, prepareBrandOrder, pickBrandOrder } from '../../api/order';
import { OrderDetailMixins } from '../../../behaviors/page';

Page({
    mixins: [OrderDetailMixins],
    data: {
        orderType: ORDER_TYPE,
        orderDetail: {},
        orderStatusInfo: {},
        showPickModal: false, // 提货码
        showExpressModal: false, // 发货快递选择
        activityUrl: '',
    },
    onLoad: async function (options) {
        const { orderNumber } = options;
        await this.refreshOrderDetail(orderNumber);
    },
    refreshOrderDetail: async function (orderNumber) {
        showLoading('加载中...');
        console.log('OrderDetail => 订单号为：', orderNumber);
        const orderDetail = await getBrandOrderDetail(orderNumber);
        console.log('OrderDetail => 订单详情为：', orderDetail);
        this.setData({
            orderDetail,
            orderStatusInfo: handleBrandOrderStatus(orderDetail),
            activityUrl: `${PACKAGE_ACTIVITY_PAGE['detail']}?activityId=${orderDetail.activityId}`,
        });
        hideLoading();
    },
    // 取消订单
    invokeCancelOrder: function () {},
    // 提醒缺货
    invokeNoticeAction: function () {},
    // 确认操作
    invokeConfirmAction: async function () {
        const { deliveryMode, serialNumber } = this.data.orderDetail;
        const isExpress = deliveryMode === DELIVERY_TYPE_ENUM.express;
        if (isExpress) {
            // 确认发货
            this.setData({
                showExpressModal: true,
            });
        } else {
            // 确认备货
            const res = await prepareBrandOrder(serialNumber);
            if (res) {
                await this.refreshOrderDetail(serialNumber);
            }
        }
    },
    expressModalAction: async function (event) {
        const { platform, number } = event.detail;
        const { serialNumber } = this.data.orderDetail;
        if (!platform || !number) {
            tipError('快递信息不完整');
            return;
        }
        const param = {
            platform,
            platformOrderNum: number,
        };
        const res = await deliverBrandOrder(serialNumber, param);
        if (!res) {
            tipError('发货失败');
            setTimeout(async () => {
                await this.refreshOrderDetail(serialNumber);
            }, 2000);
            return;
        };
        await this.refreshOrderDetail(serialNumber);
    },
    // 完成操作
    invokeComplateAction: function () {
        this.setData({
            showPickModal: true,
        })
    },
    // 提交确认提货信息
    pickModalAction: async function (event) {
        const code = event.detail;
        if (!code) {
            tipError('请输入提货码');
            return;
        }
        const { serialNumber } = this.data.orderDetail;
        const res = await pickBrandOrder(serialNumber, code);
        if (!res.flag) {
            tipError(res.msg);
            setTimeout(async () => {
                await this.refreshOrderDetail(serialNumber);
            }, 2000);
            return;
        };
        await this.refreshOrderDetail(serialNumber);
    },
})